package com.songlanyun.modules.ship.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.songlanyun.common.drive.RootEntity;
import com.songlanyun.common.enums.OrderConstant;
import com.songlanyun.modules.order.entity.Order;
import com.songlanyun.modules.order.service.OrderService;
import com.songlanyun.modules.rabbitmq.util.SpringContextUtil;
import com.songlanyun.modules.ship.model.dto.OrderPackageItemVo;
import com.songlanyun.modules.ship.service.OrderPackageExpressService;
import com.songlanyun.modules.ship.service.OrderPackageItemService;
import com.songlanyun.modules.ship.service.OrderPackagePickupService;
import com.songlanyun.modules.ship.service.OrderPackageService;
import lombok.Data;

import javax.validation.constraints.NotNull;

/**
 * 订单的发货包裹
 *
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2020-11-19 13:49:33
 */
@Data
@TableName("yxt_sale_order_package")
public class OrderPackage extends RootEntity<Long,OrderPackage> implements Serializable {
	private static final long serialVersionUID = 1L;
	{
		thirdShip = false;
	}
	/**
	 *
	 */
	@TableId
	private Long id;

	/**
	 * 所属订单id
	 */
	@NotNull(message = "所属订单id不能为空")
	private Long orderId;

	/**
	 * 所属订单编号
	 */
	private String orderCode;

	@JsonIgnore
	@TableField(exist = false)
	private Order order;

	public Order loadOrder()
	{
		if(order ==null)
		{
			OrderService service = (OrderService)SpringContextUtil.getBean("orderService");
			order = service.getById(this.getOrderId());
		}
		return order;

	}

	/**
	 * 0物流包裹，1自提包裹
	 */
	private OrderConstant.GoodsPackageType goodsPackageType;
	/**
	 * 0等待收货，1已收货，2过期
	 */
	private OrderConstant.GoodsPackageStatus goodsPackageStatus;
	/**
	 * 包裹只有一次延长收货机会
	 */
	private Boolean useDelayReceipt;
	/**
	 * 包裹收货过期时间 (到期后自动确认收货)
	 */
	private Long receiptExpireTime;
	/**
	 * 包裹收货时间
	 */
	private Date receiptTime;
	/**
	 *
	 */
	@TableField(fill = FieldFill.INSERT)
	private Date createTime;
	/**
	 *
	 */
	@TableField(fill = FieldFill.INSERT_UPDATE)
	private Date updateTime;

	@Override
	public Long getIdentity() {
		return getId();
	}

	/**
	 * 包裹的物流信息
	 */
	@TableField(exist = false)
	public OrderPackageExpress express;

	public OrderPackageExpress loadExpress()
	{
		if(express==null)
		{
			OrderPackageExpressService orderPackageExpressService = (OrderPackageExpressService)SpringContextUtil.getBean("orderPackageExpressService");
			express = orderPackageExpressService.getOne(new LambdaQueryWrapper<OrderPackageExpress>()
					.eq(OrderPackageExpress::getGoodsPackageId, this.getId())
					.last("limit 1"));
		}
		return express;
	}

	/**
	 * 包裹的自提信息
	 */
	@TableField(exist = false)
	public OrderPackagePickup pickup;

	public OrderPackagePickup loadPickup()
	{
		if(pickup==null)
		{
			OrderPackagePickupService orderPackagePickupService = (OrderPackagePickupService)SpringContextUtil.getBean("orderPackagePickupService");
			pickup = orderPackagePickupService.getOne(new LambdaQueryWrapper<OrderPackagePickup>()
					.eq(OrderPackagePickup::getGoodsPackageId, this.getId())
					.last("limit 1"));
		}
		return pickup;
	}


	@TableField(exist = false)
	private List<OrderPackageItemVo> orderPackageItems;

	public List<OrderPackageItemVo> loadOrderPackageItems() {
		OrderPackageItemService orderPackageItemService = (OrderPackageItemService) SpringContextUtil.getBean("orderPackageItemService");
		orderPackageItems = orderPackageItemService.loadOrderPackageItemVos(this.getId());
		return orderPackageItems;
	}

	/**
	 * 是否是第三方发货
	 */
	public Boolean thirdShip;

}
